package com.earthea.chatbot.service.impl;

import cn.hutool.core.util.IdUtil;
import com.earthea.chatbot.common.entity.BaseRsp;
import com.earthea.chatbot.entity.domain.Chat;
import com.earthea.chatbot.entity.response.AddChatRsp;
import com.earthea.chatbot.entity.response.ChatRsp;
import com.earthea.chatbot.mapper.ChatMapper;
import com.earthea.chatbot.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 对话管理服务
 */
@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Override
    public AddChatRsp insert(Chat chat) {
        chat.setChatId(IdUtil.simpleUUID());
        AddChatRsp addChatRsp = chatMapper.insert(chat) > 0 ? AddChatRsp.success(AddChatRsp.class) : AddChatRsp.failed(AddChatRsp.class);
        addChatRsp.setChatId(chat.getChatId());
        return addChatRsp;
    }

    @Override
    public BaseRsp update(Chat chat) {
        return chatMapper.update(chat) > 0 ? BaseRsp.success() : BaseRsp.failed();
    }

    @Override
    public BaseRsp delete(Chat chat) {
        return chatMapper.delete(chat) > 0 ? BaseRsp.success() : BaseRsp.failed();
    }

    @Override
    public ChatRsp select(Long userId) {
        List<Chat> chats = chatMapper.select(userId);
        ChatRsp rsp = BaseRsp.success(ChatRsp.class);
        rsp.setChats(chats);
        return rsp;
    }
}
